package com.press.controllers.admin;

import com.alibaba.fastjson.JSONObject;
import com.press.bean.UserBean;
import com.press.services.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by haocongshun on 2016/10/5.
 */
@Controller
@RequestMapping("/login")
public class  LoginController {
    @Resource(name = "loginService")
    private LoginService loginService;

    public LoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @RequestMapping("/adminLogin")
    public void adminLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        JSONObject json = new JSONObject();
        UserBean admin = loginService.adminLogin(username,password);
        if(admin==null){
            json.put("Msg",false);
        }else {
            json.put("Msg",true);
            admin.setUser_password(null);
            request.getSession().setAttribute("admin",admin);
        }
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }

}
